package com.lirh.mysql.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.lirh.mysql.dao.User1Dao;
import com.lirh.mysql.entity.User1Entity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.util.concurrent.TimeUnit;

@Service
public class Uesr1Service {
    @Autowired
    private User1Dao user1Dao;
    @Autowired
    private JdbcTemplate jdbcTemplate;

@Transactional
    public void test1(){
        try {
            TimeUnit.SECONDS.sleep(10);
            System.out.println("开始修改 id=24 的age");
        Wrapper<User1Entity> wrapper = new UpdateWrapper<>();
        user1Dao.update(null,((UpdateWrapper<User1Entity>) wrapper).eq("id",24).set("age",17));
            System.out.println("修改完成 id=24 的age");
            TimeUnit.SECONDS.sleep(20);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        throw new RuntimeException();

    }
    @Transactional(isolation = Isolation.READ_COMMITTED)
    public  void test2(){
        while (true){
            User1Entity id = user1Dao.selectOne(new QueryWrapper<User1Entity>().eq("id", "24"));
            System.out.println(id.getName()+" 的年龄是： "+id.getAge());
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }
    @Async
    public void testAsync(){
        System.out.println(Thread.currentThread().getName()+"   work");
        try {
            TimeUnit.SECONDS.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("end");
    }
}
